package cn.ljl.pet.service.impl;

import cn.ljl.basic.service.IBaiduAiAuditService;
import cn.ljl.basic.util.AjaxResult;
import cn.ljl.pet.domain.Pet;
import cn.ljl.pet.domain.PetDetail;
import cn.ljl.pet.domain.PetOnlineAuditLog;
import cn.ljl.pet.mapper.PetDetailMapper;
import cn.ljl.pet.mapper.PetMapper;
import cn.ljl.pet.mapper.PetOnlineAuditLogMapper;
import cn.ljl.pet.mapper.PetTypeMapper;
import cn.ljl.pet.service.IPetService;
import cn.ljl.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class PetServiceImpl extends BaseServiceImpl<Pet> implements IPetService {
  @Autowired
  private PetDetailMapper petDetailMapper;
  @Autowired
  private PetMapper petMapper;
  @Autowired
  private IBaiduAiAuditService iBaiduAiAuditService;
  @Autowired
  private PetOnlineAuditLogMapper petOnlineAuditLogMapper;
   @Transactional
    @Override
    public void save(Pet pet) {
        super.save(pet);
       PetDetail petDetail = pet.getPetDetail();
       if (petDetail != null) {
           petDetail.setPetId(pet.getId());
           petDetailMapper.save(petDetail);
       }

   }
    @Transactional
    @Override
    public void del(Long id) {
        super.del(id);
        petDetailMapper.delByPetId(id);
    }
    @Transactional
    @Override
    public void update(Pet pet) {
        super.update(pet);
        PetDetail petDetail = pet.getPetDetail();
        if(petDetail!=null){
            petDetail.setPetId(pet.getId());
            petDetailMapper.updateByPetId(petDetail);
        }
    }
    @Transactional
    @Override
    public void patchDelete(List<Long> ids) {
        super.patchDelete(ids);
        for (Long id: ids) {
            del(id);
        }
    }

    @Override
    public AjaxResult onsale(List<Long> ids) {
        for (Long id: ids) {
            Pet pet = petMapper.loadById(id);
            Boolean aBoolean = iBaiduAiAuditService.textAudit(pet.getName());
            Boolean aBoolean1 = iBaiduAiAuditService.imageAudit(pet.getResources());
        if (aBoolean && aBoolean1 ) {//审核通过
            //改上架时间，pet状态：state=1
            petMapper.updateOnsaleState(id,new Date());
            PetOnlineAuditLog log = new PetOnlineAuditLog();
            log.setPetId(id);
            log.setState(1);
            log.setNote(id+"审核成功");
            petOnlineAuditLogMapper.save(log);
        }else {
            //添加审核日志
            PetOnlineAuditLog log = new PetOnlineAuditLog();
            log.setPetId(id);
            log.setState(0);
            log.setNote(id+"审核失败");
            petOnlineAuditLogMapper.save(log);
            return AjaxResult.me().setSuccess(false).setMsg("上架失败");
        }

        }
        return new AjaxResult();
    }

    @Override
    public void offsale(List<Long> ids) {
    //该状态state=0下架时间new Date();
        petMapper.updateOffState(ids,new Date());
    }
}
